package com.adds.lvds.controller.organize;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adds.lvds.core.pingyin.PinyinUtil;
import com.adds.lvds.core.utils.CommUtils;
import com.adds.lvds.model.organize.SysOrgUser;
import com.adds.lvds.model.organize.SysUserInfo;
import com.adds.lvds.security.BaseController;
import com.adds.lvds.service.impl.organize.SysUserInfoService;

@Controller
@RequestMapping("/organize/")
public class SysUserInfoController extends BaseController<SysUserInfo> {

	@Inject
	private SysUserInfoService sysUserInfoService;

	// 加载首页
	@RequestMapping(value = "sys_user_info_list")
	public String sys_user_info_list(@Param(value = "org_id") String org_id, @Param(value = "org_name") String org_name,
			Model model) {
		model.addAttribute("org_id", org_id);
		model.addAttribute("org_name", org_name);
		return "/organize/sys_user_info_list";
	}

	/**
	 * 分页
	 * 
	 * @param request
	 * @param response
	 * @param sysUserInfo
	 * @param page
	 * @param rows
	 * @param sidx
	 * @param sord
	 */
	@ResponseBody
	@RequestMapping(value = "loadDataGrid")
	public Map<String, Object> loadDataGrid(HttpServletRequest request, HttpServletResponse response,
			SysUserInfo sysUserInfo, @Param(value = "page") String page, @Param(value = "rows") String rows,
			@Param(value = "sidx") String sidx, @Param(value = "sord") String sord) {
		Map<String, Object> map = new HashMap<String, Object>();
		String org_id = request.getParameter("org_id");
		sysUserInfo.setReserved1(org_id);
		pageView = sysUserInfoService.query(getPageView(page, rows, sidx, sord), sysUserInfo);
		map.put("rows", pageView.getRecords());
		map.put("page", pageView.getPageNow());
		map.put("total", pageView.getPageCount());
		return map;
	}

	/**
	 * 判断emp_id是否存在
	 * 
	 * @param emp_id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("checkEmpId")
	public boolean checkEmpId(String emp_id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("emp_id", emp_id);
		SysUserInfo sysUserInfo = sysUserInfoService.querySysUserInfoByMap(map);
		if (sysUserInfo == null) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 执行新增
	 * 
	 * @param sysUserInfo
	 * @return
	 */
	@ResponseBody
	@RequestMapping("add_sys_user")
	public Map<String, Object> doInsertSysUser(@Param(value = "sysUserInfo") SysUserInfo sysUserInfo,
			@Param(value = "org_id") String org_id, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		SysOrgUser sysOrgUser = new SysOrgUser();
		sysOrgUser.setEmp_id(sysUserInfo.getEmp_id());
		sysOrgUser.setOrg_id(org_id);
		sysUserInfo.setEmp_name_whole(PinyinUtil.getFullSpell(sysUserInfo.getEmp_name()));
		String result = sysUserInfoService.addUserInfo(sysUserInfo, sysOrgUser);
		String msg = "";
		switch (result) {
		case CommUtils.SUCC:
			msg = "录入成员数据成功！";
			break;
		case CommUtils.ERROR:
			msg = "录入成员数据失败！";
			break;
		}
		map.put("result", result);
		map.put("msg", msg);
		return map;
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("deletetSysUser")
	public Map<String, Object> deletetSysUser(@Param(value = "emp_id") String emp_id) {
		Map<String, Object> map = new HashMap<String, Object>();
		String result = sysUserInfoService.batchDelUserInfoByEmpId(emp_id);
		map.put("result", result);
		return map;
	}

	// 修改页面
	@RequestMapping("editSysUser")
	public String editSysUser(Model model, @Param(value = "emp_id") String emp_id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("emp_id", emp_id);
		SysUserInfo sysUserInfo = sysUserInfoService.querySysUserInfoByMap(map);
		model.addAttribute("sysUserInfo", sysUserInfo);
		return "/organize/edit_sys_user_info";
	}

	/**
	 * 修改
	 * 
	 * @param sysUserInfo
	 * @return
	 */
	@ResponseBody
	@RequestMapping("doEditSysUser")
	public Map<String, Object> doEditSysUser(@Param(value = "sysUserInfo") SysUserInfo sysUserInfo) {
		Map<String, Object> map = new HashMap<String, Object>();
		String result = sysUserInfoService.updateUserInfo(sysUserInfo);
		String msg = "";
		switch (result) {
		case CommUtils.SUCC:
			msg = "编辑成员数据成功！";
			break;
		case CommUtils.ERROR:
			msg = "编辑成员数据失败！";
			break;
		}
		map.put("result", result);
		map.put("msg", msg);
		return map;
	}

}
